<?php
$this->jQuery()->enable()->uiEnable();
$this->headLink()
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid.css'))
        ->appendStylesheet($this->baseUrl('/lib/flexigrid-1.1/css/flexigrid-custom.css'));
$this->headScript()
        ->appendFile($this->baseUrl('/lib/flexigrid-1.1/js/flexigrid.js'));
$this->pagetitle = 'Quản lý yêu cầu';
$uRole = Zend_Auth::getInstance()->getIdentity()->Role;
?>
<div id="tabs">
    <ul>
        <li><a href="#tabs-1">Yêu cầu của bạn</a></li>
        <li><a href="#tabs-2">Các yêu cầu chưa xử lý</a></li>
        <li><a href="#tabs-3">Các yêu cầu đang xử lý</a></li>
        <li><a href="#tabs-4">Tất cả các yêu cầu</a></li>
    </ul>
    <div id="tabs-1">
        <table class='gridTable1' style="display:none"></table>
    </div>
    <div id="tabs-2">
        <table class='gridTable2' style="display:none"></table>
    </div>
    <div id="tabs-3">
        <table class='gridTable3' style="display:none"></table>
    </div>
    <div id="tabs-4">
        <table class='gridTable4' style="display:none"></table>
    </div>
</div>
<div id="dialog-form" style="display: none">
    <p class="validateTips">All form fields are required.</p>
    <?php echo $this->form; ?>
</div>

<div id="dialog-confirm" title="Delete confirm?" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Information of item will be permanently deleted and cannot be recovered. Are you sure?
    </p>
</div>

<div id="dialog-confirm1" title="Accept confirm?" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Bạn đồng ý với yêu cầu này ???
    </p>
</div>
<div id="dialog-confirm2" title="Denied confirm?" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Bạn từ chối yêu cầu này ???
    </p>
</div>
<div id="dialog-confirm3" title="Success confirm?" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
        Thực sự yêu cầu này đã hoàn thành ???
    </p>
</div>
<script type="text/javascript">
    <!--
    $(document).ready(function(){
        $( "#tabs" ).tabs();
        var tips = $(".validateTips");
        var gridTable1 = $(".gridTable1");
        var gridTable2 = $(".gridTable2");
        var gridTable3 = $(".gridTable3");
        var gridTable4 = $(".gridTable4");
        $("#tabs-1").click(fDataGrid(gridTable1,'/QLTSv1/asset/request/records/mode/4'));
        $("#tabs-2").click(fDataGrid(gridTable2,'/QLTSv1/asset/request/records/mode/2'));
        $("#tabs-3").click(fDataGrid(gridTable3,'/QLTSv1/asset/request/records/mode/3'));
        $("#tabs-4").click(fDataGrid(gridTable4,'/QLTSv1/asset/request/records/mode/1'));
        var requestid = $("#RequestID"),
        maTS = $("#MaTS"),
        utype = $("#Type"),
        udetail = $("#Detail"),
        allFields = $( [] ).add(requestid).add(maTS).add(utype).add(udate).add(udetail);
                            
        function updateTips( t ) {
            tips
            .text( t )
            .addClass( "ui-state-highlight" );
            setTimeout(function() {
                tips.removeClass( "ui-state-highlight", 1500 );
            }, 500 );
        }

        function checkLength( o, n, min, max ) {
            if ( o.val().length > max || o.val().length < min ) {
                o.addClass( "ui-state-error" );
                updateTips( "Length of " + n + " must be between " +
                    min + " and " + max + "." );
                return false;
            } else {
                return true;
            }
        }

        function checkRegexp( o, regexp, n ) {
            if ( !( regexp.test( o.val() ) ) ) {
                o.addClass( "ui-state-error" );
                updateTips( n );
                return false;
            } else {
                return true;
            }
        }
        <?php if($uRole == 0 || $uRole == 1 || $uRole == 2) :?>
        function delete_record(command,grid)
        {
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                $( "#dialog-confirm" ).dialog({
                    resizable: false,
                    height:200,
                    width:500,
                    modal: true,
                    buttons: {
                        "Delete" : function() {
                            $.ajax({
                                type: "POST",
                                url: '/QLTSv1/asset/request/delete',
                                data: 'RequestID='+id,
                                success: function (data){
                                    var getData = $.parseJSON(data);
                                    var tag = $("<div title='"+getData.status+"'></div>");
                                    tag.html("<span>"+getData.msg+"</span>").dialog({
                                        resizable: false,
                                        height:200,
                                        width: 300,
                                        modal: true,
                                        buttons: {
                                            "OK": function() {
                                                $('.gridTable1').flexReload();
                                                $('.gridTable2').flexReload();
                                                $('.gridTable3').flexReload();
                                                $('.gridTable4').flexReload();
                                                $( this ).dialog( "close" );
                                            }
                                        }
                                    }).dialog("open");
                                }
                            });
                            $( this ).dialog( "close" );
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        } // delete_record
            
        function accept_record(command,grid){
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                $( "#dialog-confirm1" ).dialog({
                    resizable: false,
                    height:200,
                    width:500,
                    modal: true,
                    buttons: {
                        "Accept" : function() {
                            $.ajax({
                                type: "POST",
                                url: '/QLTSv1/asset/request/accept',
                                data: 'RequestID='+id,
                                success: function (data){
                                    var getData = $.parseJSON(data);
                                    var tag = $("<div title='"+getData.status+"'></div>");
                                    tag.html("<span>"+getData.msg+"</span>").dialog({
                                        resizable: false,
                                        height:200,
                                        width: 300,
                                        modal: true,
                                        buttons: {
                                            "OK": function() {
                                                $('.gridTable1').flexReload();
                                                $('.gridTable2').flexReload();
                                                $('.gridTable3').flexReload();
                                                $('.gridTable4').flexReload();
                                                $( this ).dialog( "close" );
                                            }
                                        }
                                    }).dialog("open");
                                }
                            });
                            $( this ).dialog( "close" );
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        } // accept_record
            
        function disaccept_record(command,grid){
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                $( "#dialog-confirm2" ).dialog({
                    resizable: false,
                    height:200,
                    width:500,
                    modal: true,
                    buttons: {
                        "Disaccept" : function() {
                            $.ajax({
                                type: "POST",
                                url: '/QLTSv1/asset/request/disaccept',
                                data: 'RequestID='+id,
                                success: function (data){
                                    var getData = $.parseJSON(data);
                                    var tag = $("<div title='"+getData.status+"'></div>");
                                    tag.html("<span>"+getData.msg+"</span>").dialog({
                                        resizable: false,
                                        height:200,
                                        width: 300,
                                        modal: true,
                                        buttons: {
                                            "OK": function() {
                                                $('.gridTable1').flexReload();
                                                $('.gridTable2').flexReload();
                                                $('.gridTable3').flexReload();
                                                $('.gridTable4').flexReload();
                                                $( this ).dialog( "close" );
                                            }
                                        }
                                    }).dialog("open");
                                }
                            });
                            $( this ).dialog( "close" );
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        } // disaccept_record
        <?php if($uRole == 2) :?>
        function upgrade_success(command,grid){
        var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                $( "#dialog-confirm3" ).dialog({
                    resizable: false,
                    height:200,
                    width:500,
                    modal: true,
                    buttons: {
                        "Success" : function() {
                            $.ajax({
                                type: "POST",
                                url: '/QLTSv1/asset/request/success',
                                data: 'RequestID='+id,
                                success: function (data){
                                    var getData = $.parseJSON(data);
                                    var tag = $("<div title='"+getData.status+"'></div>");
                                    tag.html("<span>"+getData.msg+"</span>").dialog({
                                        resizable: false,
                                        height:200,
                                        width: 300,
                                        modal: true,
                                        buttons: {
                                            "OK": function() {
                                                $('.gridTable1').flexReload();
                                                $('.gridTable2').flexReload();
                                                $('.gridTable3').flexReload();
                                                $('.gridTable4').flexReload();
                                                $( this ).dialog( "close" );
                                            }
                                        }
                                    }).dialog("open");
                                }
                            });
                            $( this ).dialog( "close" );
                        },
                        Cancel: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            });
        } // upgrade_success
        <?php endif; ?>      
<?php endif; ?>    
        function detail_record(command,grid){
            var record_count = $('.trSelected',grid).length;
            if (0 == record_count)
            {
                alert('Please select a record first by clicking on the appropriate row.');
                return;
            }
            $('.trSelected',grid).each(function(){
                var id = this.id.substr(3);
                //            window.location = '/QLTSv1/user/user/detail/UserID/'+id;
                $.ajax({
                    type: "POST",
                    url: "/QLTSv1/asset/request/detail",
                    data: "RequestID="+id,
                    success: function(data) {
                        //                        alert(data);
                        var getData = $.parseJSON(data);
                        if(getData.status == 'success'){
                            // get user info success
                            var tag = $("<div title='Request information'></div>");
                            tag.html(
                            '<table><tr><td>Người yêu cầu</td><td>:</td><td>'+getData.data.Username+
                                '</td></tr><tr><td>Nguoi yeu cau</td><td>:</td><td>'+getData.data.Ma_tai_san+
                                '</td></tr><tr><td>Tên tài sản</td><td>:</td><td>'+getData.data.TenTS+
                                '</td></tr><tr><td>Loại yêu cầu</td><td>:</td><td>'+getData.data.Type+
                                '</td></tr><tr><td>Ngày yêu cầu</td><td>:</td><td>'+getData.data.Date+
                                '</td></tr><tr><td>Chi tiết yêu cầu</td><td>:</td><td>'+getData.data.Detail+
                                '</td></tr><tr><td>Tình trạng yêu cẩu</td><td>:</td><td>'+getData.data.Accept+
                                '</td></tr></table>'
                        ).dialog({
                                resizable: false,
                                height:350,
                                width: 300,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }else{
                            // get user info error
                            var tag = $("<div title='Error'></div>");
                            tag.html("<span>"+getData.msg+"</span>").dialog({
                                resizable: false,
                                height:350,
                                width: 450,
                                modal: true,
                                buttons: {
                                    "OK": function() {
                                        $( this ).dialog( "close" );
                                    }
                                }
                            }).dialog("open");
                        }
                    
                    
                    }
                });
            });
        } // detail_record
        
        function add_record() {
            $("#dialog-form").attr('title', 'Create new request');
            $("#dialog-form").dialog({
                autoOpen: false,
                height: 350,
                width: 400,
                modal: true,
                buttons: {
                    "Create a request": function() {
                        $.ajax({
                            type: "POST",
                            url: "/QLTSv1/asset/request/add",
                            data: "RequestID="+requestid.val()+"&MaTS="+maTS.val()+"&Type="+utype.val()+"&Detail="+udetail.val(),
                            success: function (data){
                                var getData = $.parseJSON(data);
                                var tag = $("<div title='"+getData.status+"'></div>");
                                tag.html("<span>"+getData.msg+"</span>").dialog({
                                    resizable: false,
                                    height:200,
                                    width: 300,
                                    modal: true,
                                    buttons: {
                                        "OK": function() {
                                            $('.gridTable1').flexReload();
                                            $('.gridTable2').flexReload();
                                            $('.gridTable3').flexReload();
                                            $('.gridTable4').flexReload();
                                            $( this ).dialog( "close" );
                                        }
                                    }
                                }).dialog("open");
                            }
                        });
                        $( this ).dialog( "close" );      
                                            
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                },
                close: function() {
                    allFields.val( "" ).removeClass( "ui-state-error" );
                }
            });
            $( "#dialog-form" ).dialog( "open" );
        } // add_record
        
        function fDataGrid(gTB, t){
            gTB.flexigrid({
                url : t,
                dataType : 'json',
                colModel : [ 
                    {display: 'Người yêu cầu', name: 'Username', width: 100, sortable: true, align: 'left'},
                    {display: 'Mã TS', name: 'Ma_tai_san', width: 70, sortable: true, align: 'left'},
                    {display: 'Tên tài sản', name: 'TenTS', width: 200, sortable: true, align: 'left'},
                    {display: 'Loại yêu cầu', name: 'Type', width: 100, sortable: true, align: 'left'},
                    {display: 'Ngày yêu cầu', name: 'Date', width: 70, sortable: true, align: 'left'},
                    {display: 'Chi tiết yêu cầu', name: 'Detail', width: 300, sortable: false, align: 'left'},
                    {display: 'Tình trạng yêu cầu', name: 'Accept', width: 100, sortable: true, align: 'left'}
                ],
                buttons : [ 
                    {name : 'Add', bclass : 'add', onpress : add_record},
//                    {name : 'Delete', bclass : 'delete', onpress : delete_record}, 
                    {separator : true},
                    <?php if($uRole == 0 || $uRole == 1 || $uRole == 2) :?>
                    {name : 'Accept', bclass : 'accept', onpress : accept_record},
                    {name : 'Denied', bclass : 'disaccept', onpress : disaccept_record},
                    <?php if($uRole == 2) :?>
                    {name : 'Success', bclass : 'success', onpress : upgrade_success},
<?php endif; ?>
                    {separator : true},
                                    <?php endif; ?>
                    {name : 'Detail', bclass : 'detail', onpress : detail_record}
                ],
                searchitems : [ 
                    {display : 'Mã tài sản', name : 'Ma_tai_san', isdefault : true },
                    {display : 'Tên tài sản', name : 'TenTS' },
                    {display : 'Người yêu cầu', name : 'Username' },
                    {display : 'Ngày yêu cầu', name : 'Date' },
                    {display : 'Loại yêu cầu', name : 'Type' }
                ],
                sortname : "RequestID",
                sortorder : "asc",
                usepager : true,
                title : 'Bảng yêu cầu',
                useRp : true,
                rp : 15,
                singleSelect : true,
                showTableToggleBtn : true,
                width : 1080,
                height : 250
            });
        }
    });
    -->
</script>